package com.java28.rbac.service.impl;

import com.java28.rbac.mapper.UserMapper;
import com.java28.rbac.pojo.User;
import com.java28.rbac.pojo.UserExample;
import com.java28.rbac.pojo.UserExample.Criteria;
import com.java28.rbac.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired(required=false)
    UserMapper userMapper;


    @Override
    public User login(User obj) {
        UserExample example = new UserExample();
        Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(obj.getUsername());
        criteria.andUserpwdEqualTo(obj.getUserpwd());
        List<User> user = userMapper.selectByExample(example);
        return user.size()==0?null:user.get(0);
    }

    @Override
    public List<User> findAll(String like) {
        return userMapper.selectByLikes(like);
    }

    @Override
    public void save(User obj) {
        obj.setUserpwd("123456");
        userMapper.insertSelective(obj);
    }

    @Override
    public void deleteOne(Integer Id) {
        userMapper.deleteByPrimaryKey(Id);
    }

    @Override
    public User findOne(Integer userid) {
        return userMapper.selectByPrimaryKey(userid);
    }

    public void update(User user){
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public void deleteAll(Integer[] userid) {
        UserExample userExample = new UserExample();
        Criteria criteria = userExample.createCriteria();
        List<Integer> list = Arrays.asList(userid);
        criteria.andUseridIn(list);
        userMapper.deleteByExample(userExample);
    }
}
